<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <!-- <role-search @search="reload" /> -->
      <!-- 数据表格 -->

      <ele-pro-table ref="table"
                     :columns="columns"
                     :datasource="datasource"
                     :selection.sync="selection"
                     tool-class="ele-toolbar-form"
                     row-key="id">
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <!-- <el-button size="small"
                     type="primary"
                     icon="el-icon-plus"
                     class="ele-btn-icon"
                     @click="openEdit()">
            添加
          </el-button> -->

        </template>

        <template v-slot:img="{ row }">
          <div><img class="categoryImages"
                 :src="row.img"
                 alt="" /></div>
        </template>
        <template v-slot:content="{ row }">
          <div v-html="row.content"></div>
        </template>
        <template v-slot:status="{ row }">
          <el-switch :active-value="1"
                     :inactive-value="2"
                     v-model="row.status"
                     @change="editStatus(row)" />
        </template>
        <!-- 状态列 -->

        <template v-slot:is_agree="{ row }">
          <div>
            {{is_agreeFn(row.is_agree)  }}
          </div>
        </template>
        <template v-slot:sort="{ row }">
          <i class="sort-handle el-icon-rank ele-text-secondary"
             style="cursor: move"></i>
          <div v-if="false">{{ row }}</div>
        </template>
        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-link type="primary"
                   v-if="row.is_agree==1"
                   :underline="false"
                   icon="el-icon-edit"
                   @click="remove(row,2)">
            同意
          </el-link>
          <el-link disabled
                   v-if="row.is_agree==2"
                   :underline="false"
                   icon="el-icon-edit">
            已同意
          </el-link>
          <el-link disabled
                   v-if="row.is_agree==3"
                   :underline="false"
                   icon="el-icon-edit">
            已拒绝
          </el-link>

          <el-popconfirm class="ele-action"
                         title="确定要拒绝该条申请信息？"
                         @confirm="remove(row,3)">
            <template v-slot:reference>
              <el-link type="danger"
                       v-if="row.is_agree==1"
                       :underline="false"
                       icon="el-icon-delete">
                拒绝
              </el-link>
            </template>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <role-edit :data="current"
               :drawerEditBoolen.sync="drawerEditBoolen"
               @done="reload" />
    <!-- 权限分配弹窗 -->
  </div>
</template>
	
	<script>


// import RoleSearch from './components/role-search';
// import Sortable from 'sortablejs';
import RoleEdit from './components/role-edit';


import { checkUser, agreeCheck } from '@/api/userList/index'
import { classifyList, upClassify } from '@/api/classifyList/index'
// import {
//   sitecCategoriesDelete,
//   siteCategoriesList,
//   siteCategoriesModify,
//   siteCategoriesDetail,
//   siteBatchCategoriesDelete
// } from '@/api/serviceManagement/categoriesList';
export default {
  name: 'Recharge',
  components: {
    // RoleSearch,
    RoleEdit
  },
  data () {
    return {

      // 表格列配置
      columns: [

        // {
        //   prop: 'id',
        //   label: '排序',
        //   sortable: 'custom',
        //   showOverflowTooltip: true,
        //   minWidth: 30,
        //   align: 'center',
        //   slot: 'sort'
        // },
        {
          columnKey: 'index',
          type: 'index',
          label: '序号',
          width: 60,
          align: 'center',
          showOverflowTooltip: true,
          fixed: 'left'
        },

        {
          prop: 'check_nickname',
          label: '申请者姓名',
          sortable: 'custom',
          width: 105,
          align: 'center',
          showOverflowTooltip: true,

        },
        {
          prop: 'nickname',
          label: '被申请者',
          sortable: 'custom',
          align: 'center',
          showOverflowTooltip: true
        },
        {
          prop: 'phone',
          label: '申请的电话',
          sortable: 'custom',

          showOverflowTooltip: true,

          align: 'center'
        },
        {
          prop: 'is_agree',
          label: '申请状态',
          sortable: 'custom',

          showOverflowTooltip: true,
          align: 'center',
          slot: 'is_agree'
        },


        {
          prop: 'create_time',
          label: '创建时间',
          align: 'center',
          sortable: 'custom',
          showOverflowTooltip: true,
          formatter: (_row, _column, cellValue) => {
            return this.$util.toDateString(cellValue, 'yyyy-MM-dd HH:mm');
          }
        },
        {
          columnKey: 'action',
          label: '操作',

          align: 'center',
          resizable: false,
          slot: 'action',
          hideInSetting: true,
          showOverflowTooltip: true
        }
      ],
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,

      // 是否显示编辑弹窗
      drawerEditBoolen: false,
      // 是否显示导入弹窗
      showAuth: false,
      showChoose: false
    };
  },
  mounted () {

  },
  activated () {
    this.reload();
  },
  computed: {
    is_agreeFn: function () {
      return function (status) {

        let orderStatus = '';
        // status订单状态（0.待付款 1.待接单 2.已接单 3.服务中 4.已完成 5.已取消/退款)
        orderStatus =
          status == 0 ? '全部' : status == 1 ? '申请中' : status == 2 ? '已同意' : '驳回'

        return orderStatus;
      };
    },
  },

  methods: {
    // rowDrop () {
    //   const tbody = document.querySelector('.el-table__body-wrapper tbody');
    //   const _this = this;
    //   Sortable.create(tbody, {
    //     forceFallback: true,
    //     dragClass: 'drag',
    //     delay: 100,
    //     animation: 1000,
    //     onEnd ({ newIndex, oldIndex }) {
    //       console.log(newIndex);
    //       console.log(oldIndex);
    //       //更换表格数据 后续拖动坐标才会正确
    //       let data = _this.$refs.table.getData();

    //       let nid = data[newIndex].id;
    //       let oid = data[oldIndex].id;

    //       const currRow = data.splice(oldIndex, 1)[0];
    //       data.splice(newIndex, 0, currRow);

    //       console.log(data);

    //       //   sortChange(data)
    //       //     .then((msg) => {

    //       //     })
    //       //     .catch((e) => {
    //       //       _this.$refs.table.reload();
    //       //     });

    //       //去数据库更换排序

    //       console.log(_this.$refs.table.getData());
    //     }
    //   });
    // },

    /* 表格数据源 */
    datasource ({ page, limit, where }) {
      return checkUser({
        page,
        limit,
        ...where,

      });
    },
    /* 刷新表格 */
    reload (where) {
      this.$refs.table.reload({ page: 1, where: where });
    },
    /* 显示编辑 */
    async openEdit (row) {
      console.log(row);
      if (row?.id) {
        // const res = await siteCategoriesDetail(row.id);

        this.current = row;
        // 用户余额管理
        this.drawerEditBoolen = !this.drawerEditBoolen;
      } else {
        this.drawerEditBoolen = !this.drawerEditBoolen;
        this.current = null;
      }
    },

    /* 更改状态 */
    editStatus (row) {
      const loading = this.$loading({ lock: true });
      upClassify({
        id: row.id,
        status: row.status,

      })
        .then((msg) => {
          console.log(msg)
          loading.close();
          this.$message.success(msg.message);
          this.reload();
        })
        .catch((e) => {
          loading.close();
          row.status = !row.status ? 1 : 2;
          this.$message.error(e.message);
        });
    },

    /* 显示分配权限 */
    openAuth (row) {
      this.current = row;
      this.showAuth = true;
    },
    /* 删除 */
    remove (row, is_agree) {
      const loading = this.$loading({ lock: true });
      agreeCheck({
        is_agree: is_agree,
        id: row.id,
      })
        .then((msg) => {
          loading.close();
          this.$message.success(msg.message);
          this.reload();
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    },
    /* 批量删除 */
    // removeBatch () {
    //   if (!this.selection.length) {
    //     this.$message.error('请至少选择一条数据');
    //     return;
    //   }
    //   console.log(this.selection);
    //   this.$confirm('确定要删除选中的信息吗?', '提示', {
    //     type: 'warning'
    //   })
    //     .then(() => {
    //       const loading = this.$loading({ lock: true });
    //       console.log(this.selection.map((d) => d.id));
    //       siteBatchCategoriesDelete(this.selection.map((d) => d.id))
    //         .then((msg) => {
    //           loading.close();
    //           this.$message.success(msg);
    //           this.reload();
    //         })
    //         .catch((e) => {
    //           loading.close();
    //           this.$message.error(e.message);
    //         });
    //     })
    //     .catch(() => { });
    // }
  }
};
	</script>
	
	<style lang="less">
.categoryImages {
  width: 50px;
  height: 50px;
}
</style>
	